/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.data;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.gadglet.core.GadgletRequestWrapper;
import com.gadglet.data.utils.PMF;
import com.gadglet.params.ProfileFields;

/**
 * Utility class for UserProfile
 * 
 */
public class UserProfilesUtils {
	static Logger log = Logger.getLogger("UserProfileUtils");

	
	/**
	 * Gets current user profile
	 * @param platformRequestWrapper
	 * @return
	 * @throws Exception
	 */
	public static UserProfile getMyProfile(
			GadgletRequestWrapper platformRequestWrapper) throws Exception {
		UserProfile profile = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserProfile.class);
		pm.currentTransaction().begin();
		try {

			query.setFilter("openSocialViewerId == openSocialViewrIdParam && consumerKey==consumerKeyParam");
			query.declareParameters("String openSocialViewrIdParam, String consumerKeyParam");
			query.setUnique(true);
			
			profile = (UserProfile) query.execute(
					platformRequestWrapper.getOpenSocialViewerId(),
					platformRequestWrapper.getOpenSocialConsumerKey());

		} catch (Exception e) {
			throw e;
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();
			pm.close();

		}

		return profile;
	}
	
	/**
	 * @param profileId
	 * @param platformRequestWrapper
	 * @return
	 * @throws Exception
	 */
	public static UserProfile getPersonProfile(String profileId,
			GadgletRequestWrapper platformRequestWrapper) throws Exception {
		UserProfile profile = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserProfile.class);
		pm.currentTransaction().begin();
		
		try {

			query.setFilter("openSocialViewerId == profileIdParam && consumerKey == consumerKeyParam");
			query.declareParameters("String profileIdParam, String consumerKeyParam");
			query.setUnique(true);
			
			
			profile = (UserProfile) query.execute(profileId,
					platformRequestWrapper.getOpenSocialConsumerKey());

		} catch (Exception e) {
			log.warning(e.getMessage());
			throw e;
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();
			pm.close();

		}


		
		return profile;
	}
	
	
	
	/**
	 * @param profileId
	 * @return
	 */
	public static UserProfile getProfileById(String profileId){
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserProfile.class);
		UserProfile profile = null;
		try {
		
			query.setFilter("profileId == nameParam ");
			query.declareParameters("String nameParam");
			query.setUnique(true);
			profile = (UserProfile) query.execute(profileId);

		} catch (Exception e) {
			log.warning(e.getMessage());
		} finally {
	
			pm.close();
			

		}
		
		return profile;
	}
	
	
	

	/**
	 * @param platformRequestWrapper
	 * @return
	 * @throws Exception
	 */
	public static UserProfile updateMyProfile(
			GadgletRequestWrapper platformRequestWrapper) throws Exception {
		UserProfile profile = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserProfile.class);

		try {
			pm.currentTransaction().begin();
			query.setFilter("profileId == nameParam");
			query.declareParameters("String nameParam");
			query.setUnique(true);
			profile = (UserProfile) query.execute(platformRequestWrapper.getCurrentUserProfileId());

			if(platformRequestWrapper.getProfileNickName()!=null)
				profile.setNickName(platformRequestWrapper.getProfileNickName());
			
			profile.setPrivacyStatus(platformRequestWrapper
					.getProfilePrivacy());
			
			if(platformRequestWrapper.getParameter(ProfileFields.PROFILE_THUMBNAILURL.getParamName())!=null)
				profile.setPhotoUrl(platformRequestWrapper.getParameter(ProfileFields.PROFILE_THUMBNAILURL.getParamName()));
				
			pm.makePersistent(profile);
			pm.currentTransaction().commit();
		} catch (Exception e) {
			log.warning(e.getMessage());
			throw e;
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();
			pm.close();

		}

		return profile;
	}

	/**
	 * @param platformRequestWrapper
	 * @return
	 * @throws Exception
	 */
	public static UserProfile createMyProfile(
			GadgletRequestWrapper platformRequestWrapper) throws Exception {
		UserProfile newProfile = null;
		UserProfile profile = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserProfile.class);

		try {

			query.setFilter("openSocialViewerId == openSocialViewrIdParam && consumerKey==consumerKeyParam");
			query.declareParameters("String openSocialViewrIdParam, String consumerKeyParam");
			query.setUnique(true);

			pm.currentTransaction().begin();

			profile = (UserProfile) query.execute(
					platformRequestWrapper.getOpenSocialViewerId(),
					platformRequestWrapper.getOpenSocialConsumerKey());

			if (profile != null)
				newProfile = profile;
			else {
				newProfile = new UserProfile(platformRequestWrapper);
				newProfile = pm.makePersistent(newProfile);

			}

			pm.currentTransaction().commit();
		} catch (Exception e) {
			log.warning(e.getMessage());
			throw e;
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();
			pm.close();
		}

		return newProfile;
	}

}
